package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.service.user.IdentityAccountService;
import tt.dz.biz.service.user.UserService;
import tt.dz.common.DzDigester;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CardErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.RPCUtil;
import tt.dz.vo.IdentityAccountVO;
import tt.dz.vo.UserBaseVO;

@Service("rpcAllotIdentityAccountService")
public class RPCAllotIdentityAccountService
  implements RPCBaseService
{
  @Resource(name="identityAccountService")
  private IdentityAccountService identityAccountService;
  @Resource(name="userService")
  private UserService userService;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
    throws Exception
  {
    String name = message.getName();
    Object reqJson = ((GenericRecord)request).get("req");
    String resJson = "";
    if ("allotIdentityAccount".equals(name)) {
      resJson = allotIdentityAccount(reqJson.toString());
    }
    return resJson;
  }
  
  public String allotIdentityAccount(String reqJson)
    throws Exception
  {
    JSONObject resObj = null;
    try
    {
      JSONObject reqObj = JSON.parseObject(reqJson);
      String userId = reqObj.getString("userId");
      String allotChannel = reqObj.getString("allotChannel");
      String cardNo = reqObj.getString("cardNo");
      String cardPassword = reqObj.getString("cardPassword");
      String sysCode = reqObj.getString("sysCode");
      if ((StringUtils.isBlank(userId)) || (StringUtils.isBlank(cardNo)) || (StringUtils.isBlank(sysCode)) || (
        (!"scan".equals(allotChannel)) && (StringUtils.isBlank(cardPassword))))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "必传参数有缺失！");
        
        return JSON.toJSONString(resObj);
      }
      IdentityAccountVO paramIdentityAccount = new IdentityAccountVO();
      paramIdentityAccount.setCardNo(cardNo);
      if (StringUtils.isNotBlank(cardPassword)) {
        paramIdentityAccount.setPassword(
          DzDigester.desEncrypt(cardPassword, this.identityAccountService.getIdentityAccountKey()));
      }
      UserBaseVO destUser = this.identityAccountService.queryUserByIdentityAccount(paramIdentityAccount);
      if (destUser == null)
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CardErrorCode.IDENTITY_CARD_UN_EXIST.value(), "传入的身份识别卡，卡号密码不匹配或者卡号不存在！");
        
        return JSON.toJSONString(resObj);
      }
      UserBaseVO paramUser = new UserBaseVO();
      paramUser.setUserId(userId);
      UserBaseVO sourceUser = this.userService.querySingleUser(paramUser);
      boolean excuteFlag = this.userService.allotUser(destUser, sourceUser, sysCode);
      if (excuteFlag) {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(1), null, null);
      } else {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), "绑定身份识别卡异常,请稍后重试！");
      }
    }
    catch (JSONException e)
    {
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.FORMAT_ERROR.value(), DzErrorCode.CommonErrorCode.FORMAT_ERROR
        .getDesc());
    }
    catch (Exception e)
    {
      throw e;
    }
    return JSON.toJSONString(resObj);
  }
}
